// 引入axios
import axios from "axios"
import qs from "qs"

// 设置axios默认地址
axios.defaults.baseURL = "http://110.41.43.118:3008"

//*请求网络数据
//请求商品导航分类
export const getCatData = async (): Promise<any> =>{
  const result = await axios.get("/api_cat");
  if(result.data.code != 0 ){
    console.log("getCatData方法错误");
    console.log(result);
  }
  return result.data.data;
}
//请求商品数据
export const getGoodsData = async (data:object = {}): Promise<any> =>{
  const result = await axios.get("/api_goods",{params : data});
  if(result.data.code != 0 ){
    console.log("getGoodsData方法错误");
    console.log(result);
  }
  return result.data.data;
}
//请求小分类
export const getClassifyData = async(): Promise<any> =>{
  const result = await axios.get("/api_classify");
  if(result.data.code != 0 ){
    console.log("getClassifyData方法错误");
    console.log(result);
  }
  return result.data.data;
}
//搜索
export const getSearchData = async(data:object = {}): Promise<any> =>{
  const result = await axios.get("/api_search",{params : data});
  if(result.data.code != 0 ){
    console.log("getSearchData方法错误");
    console.log(result);
  }
  return result.data.data;
}
//登录注册检查用户名方法
export const getUserData = async(data:object): Promise<any> =>{
  return await axios.post("/api_user", qs.stringify(data));
}
//加入删除查看更新清空购物车方法
export const getCartData = async(data:object): Promise<any> =>{
  let result = await axios.post("/api_cart", qs.stringify(data));
  return result.data;
}
//用户地址列表 新增用户地址 删除用户地址 设置默认地址 设置当前收货地址
export const getAddressData = async(data:object): Promise<any> =>{
  let result = await axios.post("/api_address", qs.stringify(data));
  console.log(result.data);
  return result.data;
}
//加入结算清单获取结算清单方法
export const getSettlementDate = async(data:object): Promise<any> =>{
  let result = await axios.post("/api_settlement", qs.stringify(data));
  console.log(result.data);
  return result.data;
}
//地址逆解析
export const geocodeAddress = async(address:string): Promise<any> =>{
  let result = await axios.get('https://restapi.amap.com/v3/geocode/geo', {
    params: {
      address: address,
      key: "3c62f029e64d1daaf9ae95545c610075"
    }
  });
  console.log(result.data.geocodes[0]);
  return result.data.geocodes[0];
}

// 周边搜索
export const geoAddress = async(address:string): Promise<any> =>{
  let aa = await geocodeAddress(address);
  let result = await axios.get('https://restapi.amap.com/v3/place/around?parameters', {
    params: {
      key: "3c62f029e64d1daaf9ae95545c610075",
      location: aa.location,
      offset: "5",
      radius: "500"
    }
  });
  console.log(result.data);
  return result.data;
}
//请求本地数据
export const getLocalData = async (url:string): Promise<any> => {
    const response = await fetch('./src/assets/json/'+ url);
    const data = await response.json();
    return data;
};

//自写接口
// 请求钱包数据
export const getWalletDate = async(data:object): Promise<any> =>{
  let result = await axios.post("http://localhost:3000/api_wallet", qs.stringify(data));
  console.log(result.data.wallet.balance);
  return result.data.wallet.balance;
}
// post修改或添加订单数据
export const postOrderDate = async(data:object): Promise<any> =>{
  let result = await axios.post("http://localhost:3000/api_order", qs.stringify(data));
  console.log(result.data);
  return result.data;
}
// get 获取token值全部订单数据
export const getOrderDate = async(data: string): Promise<any> =>{
  let result = await axios.get("http://localhost:3000/api_order", {params:{
    token: data
  }});
  console.log(result.data);
  return result.data;
}